import pytest
from appium.webdriver.common.mobileby import MobileBy
from selenium.common.exceptions import NoSuchElementException

from APP_UI_TEST.contacts_podemo.base.base_page import BasePage


class AddMemberPage(BasePage):
    """ 添加成员页 """

    def add_member(self, name, phone_number):
        """输入姓名、手机号、取消邀请通知，点击保存"""
        self.driver.find_element(MobileBy.XPATH,
                                 "//*[contains(@text,'姓名')]/following-sibling::*[@text='必填']").send_keys(name)
        # 先定位到手机，然后定位到其父节点，从父节点相对位置往下查找所有符合text=‘必填’的控件
        self.driver.find_element(MobileBy.XPATH, "//*[contains(@text,'手机')]/..//*[@text='必填']").send_keys(phone_number)
        # 判断发送邀请通知选项是否已选中，如果已选中，则点击取消
        selected = self.driver.find_element(MobileBy.XPATH, "//*[contains(@text,'发送邀请通知')]").get_attribute("selected")
        if selected == "true":
            self.driver.find_element(MobileBy.XPATH, "//*[contains(@text,'发送邀请通知')]").click()

        self.driver.find_element(MobileBy.XPATH, "//*[@text='保存']").click()

        return self

    def get_add_result(self):
        """ 获取添加成员结果:toast """
        try:
            toast_text = self.driver.find_element(MobileBy.XPATH, "//*[@class='android.widget.Toast']").text
            return toast_text
        except NoSuchElementException:
            if "手机已存在于通讯录，无法添加" in self.driver.page_source:
                return "手机已存在于通讯录，无法添加"
